package dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import pojo.User;

import java.util.List;

public interface UserMapper {
    @Select("SELECT * FROM user")
    List<User> findAll();

    @Select("SELECT * FROM user WHERE user_id = #{userId}")
    User findById(Integer userId);

    @Insert("INSERT INTO user(" +
            "user_id, user_name, telephone, password, real_name, " +
            "user_role, last_login, login_count, create_time) " +
            "VALUES(#{userId}, #{userName}, #{telephone}, #{password}, #{realName}, " +
            "#{userRole}, #{lastLogin}, #{loginCount}, #{createTime})")
    int addUser(User user);

    @Update("UPDATE user SET " +
            "user_name = #{userName}, " +
            "telephone = #{telephone}, " +
            "password = #{password}, " +
            "real_name = #{realName}, " +
            "user_role = #{userRole}, " +
            "last_login = #{lastLogin}, " +
            "login_count = #{loginCount}, " +
            "create_time = #{createTime} " +
            "WHERE user_id = #{userId}")
    int updateUser(User user);

    @Delete("DELETE FROM user WHERE user_id = #{userId}")
    int deleteUser(Integer userId);

    // 根据用户名查询用户
    @Select("SELECT * FROM user WHERE user_name = #{userName}")
    User findByUserName(String userName);

    // 检查用户名是否存在
    @Select("SELECT COUNT(1) FROM user WHERE user_name = #{userName}")
    int checkUserNameExists(String userName);
}